package club.dataresources.controller;

import club.dataresources.common.R;
import club.dataresources.entity.Customer;
import club.dataresources.entity.Menu;
import club.dataresources.entity.Order;
import club.dataresources.service.CustomerService;
import club.dataresources.service.MenuService;
import club.dataresources.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户个人中心 服务(需要先进行登录)
 *
 * @author
 * @create 2018-11-12 10:59
 **/
@RequestMapping(value = "/personal")
@Controller
public class PersonalController {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private OrderService orderService;

    /**
     * 显示个人中心 （顾客信息，自定义套餐，订单历史）
     */
    @RequestMapping("/getPersonal")
    public @ResponseBody
    R getPersonal(HttpServletRequest request, HttpServletResponse response){
        System.out.println("====================显示个人中心 （顾客信息，自定义套餐，订单历史）======================");
        try{
            //Integer id = (Integer)request.getSession().getAttribute("customerId");
            Customer cu = (Customer)request.getSession().getAttribute("customerLoginFlag");
            Integer id = cu.getId();
            Customer customer = customerService.findCuById(id);

            Integer menuBy = id;
            List<Menu> liMenu = menuService.findMenuByMenuBy(id);

            Integer customerId = id;
            List<Order> liOrder = orderService.findOrderByCustomerId(customerId);

            Map<String,Object> map = new HashMap<String,Object>();
            map.put("customer",customer);
            map.put("liMenu",liMenu);
            map.put("liOrder",liOrder);

            return R.ok().put("code",200).put("data",map).put("msg","显示完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器错误");
        }
    }

    /**
     * 用户修改个人信息 密码
     */
    @RequestMapping("/editPersonal")
    public @ResponseBody
    R editPersonal(@RequestBody Customer customer){
        System.out.println("=================用户修改个人信息 密码==============");
        try{
            if(customerService.updateCustomer(customer)){
                return R.ok().put("code",200).put("msg","修改成功");
            }else{
                return R.error().put("code",500).put("'msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }

    /**
     * 再来一单
     */
    @RequestMapping("/moreOrder")
    public @ResponseBody
    R moreOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("menuId") Integer menuId){
        System.out.println("=================用户继续选择上次的套餐下单==============");
        try{
            Integer id = (Integer)request.getSession().getAttribute("customerId");
            if(id!=null && !id.equals(" ")){
                Order order = orderService.getMenuOrder(menuId,id);
                if(orderService.addOrder(order)){
                    return R.ok().put("code",200).put("msg","套餐已加入订单");
                }else{
                    return R.error().put("code",500).put("msg","套餐加入订单失败");
                }
            }else {
                return R.error().put("code",500).put("msg","customerId为null");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().put("code",500).put("msg","后台服务器异常");
        }
    }
}
